package org.misalen.web.admin.controllers;

import java.util.Date;
import java.util.List;

import org.misalen.web.admin.util.ServerResult;
import org.misalen.web.database.model.mysql.sys.SysAdmin;
import org.misalen.web.database.model.mysql.sys.SysAdminRole;
import org.misalen.web.database.model.mysql.sys.SysRole;
import org.misalen.web.database.repository.mysql.SysAdminRepository;
import org.misalen.web.database.util.PaginationFrom;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value = "/sys-admin")
public class SysAdminController extends BaseController {

	@Autowired
	protected SysAdminRepository sysAdminRepository;

	/**
	 * 列表页
	 * 
	 * @return 列表目标页
	 */
	@RequestMapping(value = "/", method = { RequestMethod.GET })
	public ModelAndView list() {
		ModelAndView modelAndView = new ModelAndView("/sysAdmin/list");
		return modelAndView;
	}

	/**
	 * 列表
	 * 
	 * @param 搜索条件
	 * @param 分页/排序条件
	 * @return 查询结果
	 */
	@RequestMapping(value = "/", method = { RequestMethod.POST })
	public @ResponseBody ServerResult listPost(SysAdmin sysAdmin, PaginationFrom<SysAdmin> pagination) {
		PaginationFrom<SysAdmin> from = sysAdminRepository.findPageByBean(sysAdmin, pagination);
		return renderSuccess(from);
	}

	/**
	 * 详情
	 * 
	 * @param 主键
	 * @return 单条详情目标页
	 */
	@RequestMapping(value = "/{primaryKey}", method = { RequestMethod.GET })
	public ModelAndView details(@PathVariable String primaryKey) {
		ModelAndView modelAndView = new ModelAndView("/sysAdmin/details");
		modelAndView.addObject("info", sysAdminRepository.get(SysAdmin.class, primaryKey));
		return modelAndView;
	}

	/**
	 * 添加页面
	 * 
	 * @return 添加目标页
	 */
	@RequestMapping(value = "/add", method = { RequestMethod.GET })
	public ModelAndView add() {
		ModelAndView modelAndView = new ModelAndView("/sysAdmin/add");
		List<SysRole> role = sysRoleRepository.findByAvailable();
		modelAndView.addObject("role", role);
		return modelAndView;
	}

	/**
	 * 添加动作
	 * 
	 * @param 信息
	 * @return 添加后的信息
	 */
	@RequestMapping(value = "/add", method = { RequestMethod.POST })
	public @ResponseBody ServerResult addPost(SysAdmin sysAdmin, String roleId) {
		sysAdmin.setAddTime(new Date());
		sysAdminRepository.save(sysAdmin);
		SysAdminRole adminRole = new SysAdminRole();
		adminRole.setAddTime(new Date());
		adminRole.setAdminId(sysAdmin.getPrimaryKey());
		adminRole.setRoleId(roleId);
		sysAdminRoleRepository.save(adminRole);
		return renderSuccess(sysAdmin);
	}

	/**
	 * 更新页面
	 * 
	 * @param 主键
	 * @return 更新目标页
	 */
	@RequestMapping(value = "/{primaryKey}/update", method = { RequestMethod.GET })
	public ModelAndView update(@PathVariable String primaryKey) {
		ModelAndView modelAndView = new ModelAndView("/sysAdmin/update");
		SysAdmin sysAdmin = sysAdminRepository.get(SysAdmin.class, primaryKey);
		SysAdminRole adminRole = sysAdminRoleRepository.getByAdmin(sysAdmin.getPrimaryKey());
		SysRole adminrole = sysRoleRepository.get(SysRole.class, adminRole.getRoleId());
		List<SysRole> role = sysRoleRepository.findByAvailable();
		modelAndView.addObject("role", role);
		modelAndView.addObject("info", sysAdmin);
		modelAndView.addObject("adminrole", adminrole);
		return modelAndView;
	}

	/**
	 * 更新动作
	 * 
	 * @param 信息
	 * @return 更新后的信息
	 */
	@RequestMapping(value = "/update", method = { RequestMethod.POST })
	public @ResponseBody ServerResult updatePost(SysAdmin sysAdmin, String roleId) {
		sysAdminRepository.updateNotNull(sysAdmin);
		sysAdminRoleRepository.deleteByAdmin(sysAdmin.getPrimaryKey());
		SysAdminRole adminRole = new SysAdminRole();
		adminRole.setAddTime(new Date());
		adminRole.setAdminId(sysAdmin.getPrimaryKey());
		adminRole.setRoleId(roleId);
		sysAdminRoleRepository.save(adminRole);
		return renderSuccess(sysAdmin);
	}

	/**
	 * 删除
	 * 
	 * @param 主键
	 * @return 删除结果
	 */
	@RequestMapping(value = "/{primaryKey}/del", method = { RequestMethod.GET })
	public @ResponseBody Boolean del(@PathVariable String primaryKey) {
		sysAdminRoleRepository.deleteByAdmin(primaryKey);
		sysAdminRepository.delete(SysAdmin.class, primaryKey);
		return true;
	}
}
